#include "MainMenuState.h"
#include "SinglePlayerState.h"
#include "CreditsMenuState.h"
#include "HowToPlayMenuState.h"
#include "OptionsMenuState.h"
#include "MultiplayerState.h"
#include "LevelSelectState.h"
#include "Game.h"
#include "AnimationSystem.h"
#include "AnchorPointAnimation.h"
#include "AnimInfo.h"
#include "BitmapFont.h"
#include "CSGD_DirectInput.h"
#include "CSGD_TextureManager.h"
#include "CSGD_XAudio2.h"
#include "EventManager.h"
#include "../resource.h"
/////////////////////////////////////////////////////////////////////////////
// Function		: CMainMenuState
// Notes		: 
/////////////////////////////////////////////////////////////////////////////
CMainMenuState::CMainMenuState(void)
{
	m_nCursor = 0;
	m_nBackgroundImg = -1;
	m_nTitle = -1;

	CEventManager::GetInstance()->RegisterListener("TestTrigger",this);
	TestBool = false;
	InWin = InLose = false;
}

/////////////////////////////////////////////////////////////////////////////
// Function		: ~CMainMenuState
// Notes		: 
/////////////////////////////////////////////////////////////////////////////
CMainMenuState::~CMainMenuState(void)
{
}
/////////////////////////////////////////////////////////////////////////////
// Function		: GetInstance
// Return		: CMainMenuState* 
// Notes		: 
/////////////////////////////////////////////////////////////////////////////
CMainMenuState* CMainMenuState::GetInstance( void )
{
	static CMainMenuState s_Instance;

	return &s_Instance;
}
/////////////////////////////////////////////////////////////////////////////
// Function		: Enter
// Notes		: 
/////////////////////////////////////////////////////////////////////////////
void CMainMenuState::Enter( void )
{
	m_nBackgroundImg = CSGD_TextureManager::GetInstance()->LoadTexture(_T("resource/Images/MenuBackground.png"));	

	m_nBackgroundImg = CSGD_TextureManager::GetInstance()->LoadTexture(_T("resource/Images/Background.png"));	
	m_nTitle = CSGD_TextureManager::GetInstance()->LoadTexture(_T("resource/Images/tuglogo.png"));	
	SplashWin = CSGD_TextureManager::GetInstance()->LoadTexture(_T("resource/Images/Lose.bmp"));	
	SplashLose = CSGD_TextureManager::GetInstance()->LoadTexture(_T("resource/Images/Win.bmp"));	

	AnimTest = new CAnimationSystem();
	AnimTest->LoadAnimation("InEngineXMLTestAnim.xml");
	info = new CAnimInfo();
	info->SetCurrAnim(0);

	AnchorTest = AnimTest->GetAnimations()[info->GetCurrAnim()];
	int test = AnimTest->GetAnimations()[info->GetCurrAnim()]->GetImageID();
	AnchorTest->SetImageID(test);
	AnchorTest->Play(false,true,*info);

	if(COptionsMenuState::GetInstance()->ReturnEng())
	{
		LoadString(CGame::GetInstance()->ReturnInsance(), ID_Eng_SinglePlayer, SinglePlayer, 40);
		LoadString(CGame::GetInstance()->ReturnInsance(), ID_Eng_Multiplayer, MultiPlayer, 40);
		LoadString(CGame::GetInstance()->ReturnInsance(), ID_Eng_HowToPlay, HowToPlay, 40);
		LoadString(CGame::GetInstance()->ReturnInsance(), ID_Eng_Options, Options, 20);
		LoadString(CGame::GetInstance()->ReturnInsance(), ID_Eng_Credits, Credits, 20);
		LoadString(CGame::GetInstance()->ReturnInsance(), ID_Eng_Exit, LangExit, 20);
		LoadString(CGame::GetInstance()->ReturnInsance(), ID_Eng_EscToMain, EscToMain, 50);
	}
	else
	{
		LoadString(CGame::GetInstance()->ReturnInsance(), ID_Swa_SinglePlayer, SinglePlayer, 40);
		LoadString(CGame::GetInstance()->ReturnInsance(), ID_Swa_Multiplayer, MultiPlayer, 40);
		LoadString(CGame::GetInstance()->ReturnInsance(), ID_Swa_HowToPlay, HowToPlay, 40);
		LoadString(CGame::GetInstance()->ReturnInsance(), ID_Swa_Options, Options, 20);
		LoadString(CGame::GetInstance()->ReturnInsance(), ID_Swa_Credits, Credits, 20);
		LoadString(CGame::GetInstance()->ReturnInsance(), ID_Swa_Exit, LangExit, 20);
		LoadString(CGame::GetInstance()->ReturnInsance(), ID_Swa_EscToMain, EscToMain, 50);
	}

	TrueEscTMain = "";

	for (int character = 0; character < 50; character++)
	{
		if(EscToMain[character] != NULL)
			TrueEscTMain += (char)EscToMain[character];
		else
			break;
	}
}

/////////////////////////////////////////////////////////////////////////////
// Function		: Exit
// Notes		: 
/////////////////////////////////////////////////////////////////////////////
void CMainMenuState::Exit ( void )
{
	CSGD_TextureManager::GetInstance()->UnloadTexture(m_nBackgroundImg);
	CSGD_TextureManager::GetInstance()->UnloadTexture(m_nTitle);

	AnchorTest->Shutdown();
	delete info;
	delete AnimTest;
}
/////////////////////////////////////////////////////////////////////////////
// Function		: Input
// Return		: bool
// Notes		: 
/////////////////////////////////////////////////////////////////////////////
bool CMainMenuState::Input( void )
{

	CSGD_DirectInput* m_pDI = CSGD_DirectInput::GetInstance();

	if(InWin || InLose)
	{
		if (m_pDI->KeyPressed(DIK_ESCAPE) || m_pDI->JoystickButtonPressed(0,0) == true )
		{
			CSGD_XAudio2::GetInstance()->SFXPlaySound(CGame::GetInstance()->GetConfirmID());
			InWin = InLose = false;
		}
	}
	else
	{
		if((m_pDI->KeyPressed(DIK_UP)  || m_pDI->JoystickDPadPressed( DIR_UP ) == true) && m_nCursor > 0)
		{
			m_nCursor--;
			CSGD_XAudio2::GetInstance()->SFXPlaySound(CGame::GetInstance()->GetMenuMovID());
		}

		else if((m_pDI->KeyPressed(DIK_DOWN) || m_pDI->JoystickDPadPressed( DIR_DOWN ) == true) && m_nCursor < 5)
		{
			m_nCursor++;
			CSGD_XAudio2::GetInstance()->SFXPlaySound(CGame::GetInstance()->GetMenuMovID());
		}
		else if (m_pDI->KeyPressed(DIK_RETURN) || m_pDI->JoystickButtonPressed(0,0) == true )
		{

			CSGD_XAudio2::GetInstance()->SFXPlaySound(CGame::GetInstance()->GetConfirmID());
			switch (m_nCursor)
			{
			case 0: 
				//CGame::GetInstance()->ChangeState(CSinglePlayerState::GetInstance());
				CGame::GetInstance()->ChangeState(CLevelSelectState::GetInstance());
				break;

			case 1:
				CGame::GetInstance()->ChangeState(CMultiplayerState::GetInstance());
				break;

			case 2:
				CGame::GetInstance()->ChangeState(CHowToPlayMenuState::GetInstance());
				break;

			case 3:
				COptionsMenuState::GetInstance()->SetPrevState(*this);
				CGame::GetInstance()->ChangeState(COptionsMenuState::GetInstance());
				break;

			case 4:
				CGame::GetInstance()->ChangeState(CCreditsMenuState::GetInstance());
				break;

			case 5:
				return false;

			default:
				m_nCursor = 0;
				break;

			}
		}
		else if(m_pDI->KeyPressed(DIK_ESCAPE))
			m_nCursor = 5;
	}

	return true;	
}
/////////////////////////////////////////////////////////////////////////////
// Function		: Update
//Parameters	: fElapsedTime - The ammount of time that has passed since last update
// Notes		: 
/////////////////////////////////////////////////////////////////////////////
void CMainMenuState::Update( float fElapsedTime )
{
	CEventManager::GetInstance()->ProcessEvents();

	AnimTest->Update(fElapsedTime,info);
}
/////////////////////////////////////////////////////////////////////////////
// Function		: Render
// Notes		: 
/////////////////////////////////////////////////////////////////////////////
void CMainMenuState::Render( void )
{
	CSGD_TextureManager::GetInstance()->Draw(m_nBackgroundImg,0,0);
	CSGD_TextureManager::GetInstance()->Draw(m_nTitle,460,-32, 0.3f, 0.3f);


	CBitmapFont* font = CGame::GetInstance()->GetFont();

	if(InWin || InLose)
	{
		if(InWin)
			CSGD_TextureManager::GetInstance()->Draw(SplashWin,0,0);
		else if(InLose)
			CSGD_TextureManager::GetInstance()->Draw(SplashLose,0,0);
		font->Draw(EscToMain,
			CGame::GetInstance()->GetScreenMidPoint(TrueEscTMain,1.0f), 568, 1.0f, D3DCOLOR_ARGB( 255, 255, 0, 0 ) );
	}
	else
	{
		font->Draw( SinglePlayer, 100, 125, 1.5f, D3DCOLOR_ARGB( 255, 255, 0, 0 ) );
		font->Draw( MultiPlayer, 100, 200,1.5f, D3DCOLOR_ARGB( 255, 255, 0, 0 ) );
		font->Draw(HowToPlay, 100, 275, 1.5f, D3DCOLOR_ARGB( 255, 255, 0, 0 ) );
		font->Draw(Options, 100, 350, 1.5f, D3DCOLOR_ARGB( 255, 255, 0, 0 ) );
		font->Draw( Credits, 100, 425, 1.5f, D3DCOLOR_ARGB( 255, 255, 0, 0 ) );
		font->Draw( LangExit, 100, 500, 1.5f, D3DCOLOR_ARGB( 255, 255, 0, 0 ) );

		font->Draw( _T(">"), 50, 125 + (75 * m_nCursor), 1.5f, D3DCOLOR_ARGB( 255, 255, 0, 0 ) );

		if(TestBool == true)
			font->Draw(_T("Animation Event Sent"), 100,100,1.0f,D3DCOLOR_ARGB( 255, 255, 255, 255 ) );

		SPosition pos;
		pos.m_nPosX = 500;
		pos.m_nPosY = 500;
		AnimTest->Render(pos,info,1.0);
	}
}

void CMainMenuState::HandleEvent(Event* Event)
{
	if(Event->GetEventIDtype() == "TestTrigger")
	{
		CBitmapFont* font = CGame::GetInstance()->GetFont();

		TestBool = !TestBool;
	}
}